package com.ozner.cup.Base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * description: NetConnectStatusRecieve,监听网络连接状态变化，重启MQTT服务
 * autour: ozner_67
 * date: 2017/8/14 17:18
 * e-mail: xinde.zhang@cftcn.com
 */

public class NetConnectStatusRecieve extends BroadcastReceiver {
    private static final String TAG = "NetConnectStatusRecieve";

    private String getConnectionType(int type) {
        String connType = "";
        if (type == ConnectivityManager.TYPE_MOBILE) {
            connType = "3G网络数据";
        } else if (type == ConnectivityManager.TYPE_WIFI) {
            connType = "WIFI网络";
        } else {
            connType = "无网络";
        }
        return connType;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = manager.getActiveNetworkInfo();
            if (networkInfo != null) {
                if (networkInfo.isConnected()) {
                    /**
                     * 这里的代码会多次刷新IO，造成APP闪退，暂时不做优化了
                     * 另外，切换网络可以自动链接设备，速度有点慢
                     */
//                    try {
//                        if (OznerDeviceManager.Instance() == null) {
//                            OznerDeviceManager.newInstance(context.getApplicationContext());
//                            OznerDeviceManager.Instance().setOwner(OznerPreference.GetValue(context, OznerPreference.UserId, null)
//                                    , OznerPreference.getUserToken(context));
//                        } else {
////                            OznerDeviceManager.Instance().ioManagerList().Stop();
//                            OznerDeviceManager.Instance().ioManagerList().Start(OznerPreference.GetValue(context, OznerPreference.UserId, null)
//                                    , OznerPreference.getUserToken(context));
//                        }
//                    } catch (Exception ex) {
//                        ex.printStackTrace();
//                        Log.e(TAG, "onReceive_Ex: " + ex.getMessage());
//                    }
//                    Log.e(TAG, "onReceive: isConnected:" + networkInfo.isConnected());
//                    Log.e(TAG, "onReceive: isAvailable:" + networkInfo.isAvailable());
//                    Log.e(TAG, "onReceive: Type:" + networkInfo.getType());
//                    Log.e(TAG, "onReceive: TypeName:" + networkInfo.getTypeName());
//                    Log.e(TAG, "onReceive: State:" + networkInfo.getState());
//                    Log.e(TAG, "getDetailedState()" + networkInfo.getDetailedState().name());
//                    Log.e(TAG, "getDetailedState()" + networkInfo.getExtraInfo());
//                    Log.e(TAG, "getType()" + networkInfo.getType());
                }
            }
        }
    }
}
